package cn.chenfeifx.checker.util;

import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.exif.ExifSubIFDDirectory;
import com.drew.metadata.jfif.JfifDirectory;
import lombok.extern.slf4j.Slf4j;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class SmartImageUtil {

    static int threshold = 10; // 定义一个容错值，用于确定是否为边缘


    /**
     * 判断图片是否有黑边
     * @param imagePath
     * @return
     */
    public static boolean detectBlackBorder(String imagePath) {
        boolean hasBlackBorder = false;

        try {
            BufferedImage image = ImageIO.read(new File(imagePath));
            int width = image.getWidth();
            int height = image.getHeight();


            for (int x = 0; x < width; x++) {
                int rgb = image.getRGB(x, 0); // 检查顶部边缘
                if (isBlack(rgb)) {
                    hasBlackBorder = true;
                }
                rgb = image.getRGB(x, height - 1); // 检查底部边缘
                if (isBlack(rgb)) {
                    hasBlackBorder = true;

                }
            }

            for (int y = 0; y < height; y++) {
                int rgb = image.getRGB(0, y); // 检查左侧边缘
                if (isBlack(rgb)) {
                    hasBlackBorder = true;
                }
                rgb = image.getRGB(width - 1, y); // 检查右侧边缘
                if (isBlack(rgb)) {
                    hasBlackBorder = true;
                }
            }
            hasBlackBorder = false;

            if (hasBlackBorder) {
                System.out.println("图片有黑边");
            } else {
                System.out.println("图片没有黑边");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

         return hasBlackBorder;


    }

    private static boolean isBlack(int rgb) {
        // 这里可以调整黑色的定义，例如通过容差值
        return ((rgb >> 16) & 0xFF) < threshold &&
                ((rgb >> 8) & 0xFF) < threshold &&
                (rgb & 0xFF) < threshold;
    }

    public static boolean isImageReadable(String imagePath) {
        File imageFile = new File(imagePath);
        if (!imageFile.exists()) {
            System.out.println("文件不存在");
            return false;
        }

        try {
            BufferedImage image = ImageIO.read(imageFile);
            if (image == null) {
                // ImageIO.read() 返回 null 如果文件不是图片或者图片损坏
                System.out.println("图片文件不可读或损坏");
                return false;
            }
            // 图片成功读取
//            System.out.println("图片文件可读");
            return true;
        } catch (IOException e) {
            System.out.println("读取图片时发生IO异常：" + e.getMessage());
            return false;
        }
    }

    public static boolean checkPPI(String filepath) throws IOException {
        try {
            File imageFile = new File(filepath);
            Metadata metadata = ImageMetadataReader.readMetadata(imageFile);
            int minPpi = 300;

            for (Directory directory : metadata.getDirectories()) {

                if("JfifDirectory".equalsIgnoreCase( directory.getClass().getSimpleName() )){
                    int xPpi = directory.getInt(JfifDirectory.TAG_RESX);
                    int yPpi =  directory.getInt(JfifDirectory.TAG_RESY);
                    System.out.println( "xPpi:"+xPpi+" yPpi:"+yPpi);
                    if (xPpi < minPpi || yPpi < minPpi) {

                    }else {
                        return true;
                    }
                }


                if("ExifIFD0Directory".equalsIgnoreCase( directory.getClass().getSimpleName() )){
                    int xPpi = directory.getInt(ExifSubIFDDirectory.TAG_Y_RESOLUTION);
                    int yPpi =  directory.getInt(ExifSubIFDDirectory.TAG_X_RESOLUTION);
                    System.out.println( "xPpi:"+xPpi+" yPpi:"+yPpi);
                    if (xPpi < minPpi || yPpi < minPpi) {

                    }else {
                        return true;
                    }
                }

                if (directory.containsTag(ExifSubIFDDirectory.TAG_BITS_PER_SAMPLE)) {
                    int yPpi = directory.getInt(ExifSubIFDDirectory.TAG_Y_RESOLUTION);
                    int xPpi = directory.getInt(ExifSubIFDDirectory.TAG_X_RESOLUTION);
                    System.out.println( "xPpi:"+xPpi+" yPpi:"+yPpi);
                    if (xPpi < minPpi || yPpi < minPpi) {
                        return false;
                    }else {
                        return true;
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) throws IOException {
        String imagePath = "/Users/zhangxin/Developer/德拜/质检/quality-inspection/images/B020-0003-0001-0005-0012.jpg";
        //EXIF
        boolean b = checkPPI(imagePath);
        System.out.println(b);

    }

    public static boolean checkColorDepth(String filepath) throws IOException {
        BufferedImage image = ImageIO.read(new File(filepath));
        int colorDepth = getBitsPerPixel(image);
        System.out.println("色彩位数: " + colorDepth + " bits per pixel");
        if(colorDepth >= 24){
            return true;
        }else {
            return false;
        }
    }
    public static int getBitsPerPixel(BufferedImage image) {
        // 获取 BufferedImage 的颜色模型
        ColorModel colorModel = image.getColorModel();
        // 获取颜色模型的样本位数
        return colorModel.getPixelSize();
    }

}
